#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/twist.hpp"

class Cmd_VelSub : public rclcpp::Node
{
public:
    rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr subscribers_;


    Cmd_VelSub() : Node("cmd_vel_subscriber")
    {
        //创建一个订阅者，订阅/cmd_vel 话题，数据类型为geometry_msgs::msg::Twist
        subscribers_ = this->create_subscription<geometry_msgs::msg::Twist>(
            "/cmd_vel",
            10,
            std::bind(&Cmd_VelSub::topic_callback, this, std::placeholders::_1));
    }

private:
    void topic_callback(const geometry_msgs::msg::Twist::SharedPtr msg)
    {
        //计算线速度的平方
        double speed_data = msg->linear.x * msg->linear.x ;
        //打印线速度的平方
        RCLCPP_INFO(this->get_logger(), "Linear.x**2 is %f", speed_data);
    }
};

int main(int argc, char * argv[])
{
    rclcpp::init(argc, argv);
    auto node = std::make_shared<Cmd_VelSub>();
    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}    